home *** CD-ROM | disk | FTP | other *** search
/ AmigActive 21 / AACD 21.iso / AACD / Utilities / Ghostscript / src / gspath.c < prev    next >
Encoding:
C/C++ Source or Header  |  2001-01-01  |  13.8 KB  |  500 lines

  1. /* Copyright (C) 1989, 1995, 1996, 1997, 1998, 1999 Aladdin Enterprises.  All rights reserved.
  2.   
  3.   This file is part of AFPL Ghostscript.
  4.   
  5.   AFPL Ghostscript is distributed with NO WARRANTY OF ANY KIND.  No author or
  6.   distributor accepts any responsibility for the consequences of using it, or
  7.   for whether it serves any particular purpose or works at all, unless he or
  8.   she says so in writing.  Refer to the Aladdin Free Public License (the
  9.   "License") for full details.
  10.   
  11.   Every copy of AFPL Ghostscript must include a copy of the License, normally
  12.   in a plain ASCII text file named PUBLIC.  The License grants you the right
  13.   to copy, modify and redistribute AFPL Ghostscript, but only under certain
  14.   conditions described in the License.  Among other things, the License
  15.   requires that the copyright notice and this notice be preserved on all
  16.   copies.
  17. */
  18.  
  19. /*$Id: gspath.c,v 1.2 2000/09/19 19:00:31 lpd Exp $ */
  20. /* Basic path routines for Ghostscript library */
  21. #include "gx.h"
  22. #include "gserrors.h"
  23. #include "gxfixed.h"
  24. #include "gxmatrix.h"
  25. #include "gscoord.h"        /* requires gsmatrix.h */
  26. #include "gspath.h"        /* for checking prototypes */
  27. #include "gzstate.h"
  28. #include "gzpath.h"
  29. #include "gxdevice.h"        /* for gxcpath.h */
  30. #include "gxdevmem.h"        /* for gs_device_is_memory */
  31. #include "gzcpath.h"
  32.  
  33. /* ------ Miscellaneous ------ */
  34.  
  35. int
  36. gs_newpath(gs_state * pgs)
  37. {
  38.     return gx_path_new(pgs->path);
  39. }
  40.  
  41. int
  42. gs_closepath(gs_state * pgs)
  43. {
  44.     gx_path *ppath = pgs->path;
  45.     int code = gx_path_close_subpath(ppath);
  46.  
  47.     if (code < 0)
  48.     return code;
  49.     if (path_start_outside_range(ppath))
  50.     path_set_outside_position(ppath, ppath->outside_start.x,
  51.                   ppath->outside_start.y);
  52.     return code;
  53. }
  54.  
  55. int
  56. gs_upmergepath(gs_state * pgs)
  57. {
  58.     return gx_path_add_path(pgs->saved->path, pgs->path);
  59. }
  60.  
  61. /* Get the current path (for internal use only). */
  62. gx_path *
  63. gx_current_path(const gs_state * pgs)
  64. {
  65.     return pgs->path;
  66. }
  67.  
  68. /* ------ Points and lines ------ */
  69.  
  70. /*
  71.  * Define clamped values for out-of-range coordinates.
  72.  * Currently the path drawing routines can't handle values
  73.  * close to the edge of the representable space.
  74.  */
  75. #define max_coord_fixed (max_fixed - int2fixed(1000))    /* arbitrary */
  76. #define min_coord_fixed (-max_coord_fixed)
  77. private void
  78. clamp_point(gs_fixed_point * ppt, floatp x, floatp y)
  79. {
  80. #define clamp_coord(xy)\
  81.   ppt->xy = (xy > fixed2float(max_coord_fixed) ? max_coord_fixed :\
  82.          xy < fixed2float(min_coord_fixed) ? min_coord_fixed :\
  83.          float2fixed(xy))
  84.     clamp_coord(x);
  85.     clamp_coord(y);
  86. #undef clamp_coord
  87. }
  88.  
  89. int
  90. gs_currentpoint(gs_state * pgs, gs_point * ppt)
  91. {
  92.     gx_path *ppath = pgs->path;
  93.     int code;
  94.     gs_fixed_point pt;
  95.  
  96.     if (path_outside_range(ppath))
  97.     return gs_itransform(pgs, ppath->outside_position.x,
  98.                  ppath->outside_position.y, ppt);
  99.     code = gx_path_current_point(pgs->path, &pt);
  100.     if (code < 0)
  101.     return code;
  102.     return gs_itransform(pgs, fixed2float(pt.x), fixed2float(pt.y), ppt);
  103. }
  104.  
  105. int
  106. gs_moveto(gs_state * pgs, floatp x, floatp y)
  107. {
  108.     gx_path *ppath = pgs->path;
  109.     gs_fixed_point pt;
  110.     int code;
  111.  
  112.     if ((code = gs_point_transform2fixed(&pgs->ctm, x, y, &pt)) < 0) {
  113.     if (pgs->clamp_coordinates) {    /* Handle out-of-range coordinates. */
  114.         gs_point opt;
  115.  
  116.         if (code != gs_error_limitcheck ||
  117.         (code = gs_transform(pgs, x, y, &opt)) < 0
  118.         )
  119.         return code;
  120.         clamp_point(&pt, opt.x, opt.y);
  121.         code = gx_path_add_point(ppath, pt.x, pt.y);
  122.         if (code < 0)
  123.         return code;
  124.         path_set_outside_position(ppath, opt.x, opt.y);
  125.         ppath->outside_start = ppath->outside_position;
  126.         ppath->start_flags = ppath->state_flags;
  127.     }
  128.     return code;
  129.     }
  130.     return gx_path_add_point(ppath, pt.x, pt.y);
  131. }
  132.  
  133. int
  134. gs_rmoveto(gs_state * pgs, floatp x, floatp y)
  135. {
  136.     gs_fixed_point dpt;
  137.     int code;
  138.  
  139.     if ((code = gs_distance_transform2fixed(&pgs->ctm, x, y, &dpt)) < 0 ||
  140.     (code = gx_path_add_relative_point(pgs->path, dpt.x, dpt.y)) < 0
  141.     ) {            /* Handle all exceptional conditions here. */
  142.     gs_point upt;
  143.  
  144.     if ((code = gs_currentpoint(pgs, &upt)) < 0)
  145.         return code;
  146.     return gs_moveto(pgs, upt.x + x, upt.y + y);
  147.     }
  148.     return code;
  149. }
  150.  
  151. int
  152. gs_lineto(gs_state * pgs, floatp x, floatp y)
  153. {
  154.     gx_path *ppath = pgs->path;
  155.     int code;
  156.     gs_fixed_point pt;
  157.  
  158.     if ((code = gs_point_transform2fixed(&pgs->ctm, x, y, &pt)) < 0) {
  159.     if (pgs->clamp_coordinates) {    /* Handle out-of-range coordinates. */
  160.         gs_point opt;
  161.  
  162.         if (code != gs_error_limitcheck ||
  163.         (code = gs_transform(pgs, x, y, &opt)) < 0
  164.         )
  165.         return code;
  166.         clamp_point(&pt, opt.x, opt.y);
  167.         code = gx_path_add_line(ppath, pt.x, pt.y);
  168.         if (code < 0)
  169.         return code;
  170.         path_set_outside_position(ppath, opt.x, opt.y);
  171.     }
  172.     return code;
  173.     }
  174.     return gx_path_add_line(pgs->path, pt.x, pt.y);
  175. }
  176.  
  177. int
  178. gs_rlineto(gs_state * pgs, floatp x, floatp y)
  179. {
  180.     gx_path *ppath = pgs->path;
  181.     gs_fixed_point dpt;
  182.     fixed nx, ny;
  183.     int code;
  184.  
  185.     if (!path_position_in_range(ppath) ||
  186.     (code = gs_distance_transform2fixed(&pgs->ctm, x, y, &dpt)) < 0 ||
  187.     /* Check for overflow in addition. */
  188.     (((nx = ppath->position.x + dpt.x) ^ dpt.x) < 0 &&
  189.      (ppath->position.x ^ dpt.x) >= 0) ||
  190.     (((ny = ppath->position.y + dpt.y) ^ dpt.y) < 0 &&
  191.      (ppath->position.y ^ dpt.y) >= 0) ||
  192.     (code = gx_path_add_line(ppath, nx, ny)) < 0
  193.     ) {            /* Handle all exceptional conditions here. */
  194.     gs_point upt;
  195.  
  196.     if ((code = gs_currentpoint(pgs, &upt)) < 0)
  197.         return code;
  198.     return gs_lineto(pgs, upt.x + x, upt.y + y);
  199.     }
  200.     return code;
  201. }
  202.  
  203. /* ------ Curves ------ */
  204.  
  205. int
  206. gs_curveto(gs_state * pgs,
  207.        floatp x1, floatp y1, floatp x2, floatp y2, floatp x3, floatp y3)
  208. {
  209.     gs_fixed_point p1, p2, p3;
  210.     int code1 = gs_point_transform2fixed(&pgs->ctm, x1, y1, &p1);
  211.     int code2 = gs_point_transform2fixed(&pgs->ctm, x2, y2, &p2);
  212.     int code3 = gs_point_transform2fixed(&pgs->ctm, x3, y3, &p3);
  213.     gx_path *ppath = pgs->path;
  214.  
  215.     if ((code1 | code2 | code3) < 0) {
  216.     if (pgs->clamp_coordinates) {    /* Handle out-of-range coordinates. */
  217.         gs_point opt1, opt2, opt3;
  218.         int code;
  219.  
  220.         if ((code1 < 0 && code1 != gs_error_limitcheck) ||
  221.         (code1 = gs_transform(pgs, x1, y1, &opt1)) < 0
  222.         )
  223.         return code1;
  224.         if ((code2 < 0 && code2 != gs_error_limitcheck) ||
  225.         (code2 = gs_transform(pgs, x2, y2, &opt2)) < 0
  226.         )
  227.         return code2;
  228.         if ((code3 < 0 && code3 != gs_error_limitcheck) ||
  229.         (code3 = gs_transform(pgs, x3, y3, &opt3)) < 0
  230.         )
  231.         return code3;
  232.         clamp_point(&p1, opt1.x, opt1.y);
  233.         clamp_point(&p2, opt2.x, opt2.y);
  234.         clamp_point(&p3, opt3.x, opt3.y);
  235.         code = gx_path_add_curve(ppath,
  236.                      p1.x, p1.y, p2.x, p2.y, p3.x, p3.y);
  237.         if (code < 0)
  238.         return code;
  239.         path_set_outside_position(ppath, opt3.x, opt3.y);
  240.         return code;
  241.     } else
  242.         return (code1 < 0 ? code1 : code2 < 0 ? code2 : code3);
  243.     }
  244.     return gx_path_add_curve(ppath,
  245.                  p1.x, p1.y, p2.x, p2.y, p3.x, p3.y);
  246. }
  247.  
  248. int
  249. gs_rcurveto(gs_state * pgs,
  250.      floatp dx1, floatp dy1, floatp dx2, floatp dy2, floatp dx3, floatp dy3)
  251. {
  252.     gx_path *ppath = pgs->path;
  253.     gs_fixed_point p1, p2, p3;
  254.     fixed ptx, pty;
  255.     int code;
  256.  
  257. /****** SHOULD CHECK FOR OVERFLOW IN ADDITION ******/
  258.     if (!path_position_in_range(ppath) ||
  259.     (code = gs_distance_transform2fixed(&pgs->ctm, dx1, dy1, &p1)) < 0 ||
  260.     (code = gs_distance_transform2fixed(&pgs->ctm, dx2, dy2, &p2)) < 0 ||
  261.     (code = gs_distance_transform2fixed(&pgs->ctm, dx3, dy3, &p3)) < 0 ||
  262.     (ptx = ppath->position.x, pty = ppath->position.y,
  263.      code = gx_path_add_curve(ppath, ptx + p1.x, pty + p1.y,
  264.                   ptx + p2.x, pty + p2.y,
  265.                   ptx + p3.x, pty + p3.y)) < 0
  266.     ) {            /* Handle all exceptional conditions here. */
  267.     gs_point upt;
  268.  
  269.     if ((code = gs_currentpoint(pgs, &upt)) < 0)
  270.         return code;
  271.     return gs_curveto(pgs, upt.x + dx1, upt.y + dy1,
  272.               upt.x + dx2, upt.y + dy2,
  273.               upt.x + dx3, upt.y + dy3);
  274.     }
  275.     return code;
  276. }
  277.  
  278. /* ------ Clipping ------ */
  279.  
  280. /* Forward references */
  281. private int common_clip(P2(gs_state *, int));
  282.  
  283. /*
  284.  * Return the effective clipping path of a graphics state.  Sometimes this
  285.  * is the intersection of the clip path and the view clip path; sometimes it
  286.  * is just the clip path.  We aren't sure what the correct algorithm is for
  287.  * this: for now, we use view clipping unless the current device is a memory
  288.  * device.  This takes care of the most important case, where the current
  289.  * device is a cache device.
  290.  */
  291. int
  292. gx_effective_clip_path(gs_state * pgs, gx_clip_path ** ppcpath)
  293. {
  294.     gs_id view_clip_id =
  295.     (pgs->view_clip == 0 || pgs->view_clip->rule == 0 ? gs_no_id :
  296.      pgs->view_clip->id);
  297.  
  298.     if (gs_device_is_memory(pgs->device)) {
  299.     *ppcpath = pgs->clip_path;
  300.     return 0;
  301.     }
  302.     if (pgs->effective_clip_id == pgs->clip_path->id &&
  303.     pgs->effective_view_clip_id == view_clip_id
  304.     ) {
  305.     *ppcpath = pgs->effective_clip_path;
  306.     return 0;
  307.     }
  308.     /* Update the cache. */
  309.     if (view_clip_id == gs_no_id) {
  310.     if (!pgs->effective_clip_shared)
  311.         gx_cpath_free(pgs->effective_clip_path, "gx_effective_clip_path");
  312.     pgs->effective_clip_path = pgs->clip_path;
  313.     pgs->effective_clip_shared = true;
  314.     } else {
  315.     gs_fixed_rect cbox, vcbox;
  316.  
  317.     gx_cpath_inner_box(pgs->clip_path, &cbox);
  318.     gx_cpath_outer_box(pgs->view_clip, &vcbox);
  319.     if (rect_within(vcbox, cbox)) {
  320.         if (!pgs->effective_clip_shared)
  321.         gx_cpath_free(pgs->effective_clip_path,
  322.                   "gx_effective_clip_path");
  323.         pgs->effective_clip_path = pgs->view_clip;
  324.         pgs->effective_clip_shared = true;
  325.     } else {
  326.         /* Construct the intersection of the two clip paths. */
  327.         int code;
  328.         gx_clip_path ipath;
  329.         gx_path vpath;
  330.         gx_clip_path *npath = pgs->effective_clip_path;
  331.  
  332.         if (pgs->effective_clip_shared) {
  333.         npath = gx_cpath_alloc(pgs->memory, "gx_effective_clip_path");
  334.         if (npath == 0)
  335.             return_error(gs_error_VMerror);
  336.         }
  337.         gx_cpath_init_local(&ipath, pgs->memory);
  338.         code = gx_cpath_assign_preserve(&ipath, pgs->clip_path);
  339.         if (code < 0)
  340.         return code;
  341.         gx_path_init_local(&vpath, pgs->memory);
  342.         code = gx_cpath_to_path(pgs->view_clip, &vpath);
  343.         if (code < 0 ||
  344.         (code = gx_cpath_clip(pgs, &ipath, &vpath,
  345.                       gx_rule_winding_number)) < 0 ||
  346.         (code = gx_cpath_assign_free(npath, &ipath)) < 0
  347.         )
  348.         DO_NOTHING;
  349.         gx_path_free(&vpath, "gx_effective_clip_path");
  350.         gx_cpath_free(&ipath, "gx_effective_clip_path");
  351.         if (code < 0)
  352.         return code;
  353.         pgs->effective_clip_path = npath;
  354.         pgs->effective_clip_shared = false;
  355.     }
  356.     }
  357.     pgs->effective_clip_id = pgs->clip_path->id;
  358.     pgs->effective_view_clip_id = view_clip_id;
  359.     *ppcpath = pgs->effective_clip_path;
  360.     return 0;
  361. }
  362.  
  363. #ifdef DEBUG
  364. /* Note that we just set the clipping path (internal). */
  365. private void
  366. note_set_clip_path(const gs_state * pgs)
  367. {
  368.     if (gs_debug_c('P')) {
  369.     extern void gx_cpath_print(P1(const gx_clip_path *));
  370.  
  371.     dlprintf("[P]Clipping path:\n");
  372.     gx_cpath_print(pgs->clip_path);
  373.     }
  374. }
  375. #else
  376. #  define note_set_clip_path(pgs) DO_NOTHING
  377. #endif
  378.  
  379. int
  380. gs_clippath(gs_state * pgs)
  381. {
  382.     gx_path cpath;
  383.     int code;
  384.  
  385.     gx_path_init_local(&cpath, pgs->memory);
  386.     code = gx_cpath_to_path(pgs->clip_path, &cpath);
  387.     if (code >= 0)
  388.     code = gx_path_assign_free(pgs->path, &cpath);
  389.     if (code < 0)
  390.     gx_path_free(&cpath, "gs_clippath");
  391.     return code;
  392. }
  393.  
  394. int
  395. gs_initclip(gs_state * pgs)
  396. {
  397.     gs_fixed_rect box;
  398.     int code = gx_default_clip_box(pgs, &box);
  399.  
  400.     if (code < 0)
  401.     return code;
  402.     return gx_clip_to_rectangle(pgs, &box);
  403. }
  404.  
  405. int
  406. gs_clip(gs_state * pgs)
  407. {
  408.     return common_clip(pgs, gx_rule_winding_number);
  409. }
  410.  
  411. int
  412. gs_eoclip(gs_state * pgs)
  413. {
  414.     return common_clip(pgs, gx_rule_even_odd);
  415. }
  416.  
  417. private int
  418. common_clip(gs_state * pgs, int rule)
  419. {
  420.     int code = gx_cpath_clip(pgs, pgs->clip_path, pgs->path, rule);
  421.     if (code < 0)
  422.     return code;
  423.     pgs->clip_path->rule = rule;
  424.     note_set_clip_path(pgs);
  425.     return 0;
  426. }
  427.  
  428. /* Establish a rectangle as the clipping path. */
  429. /* Used by initclip and by the character and Pattern cache logic. */
  430. int
  431. gx_clip_to_rectangle(gs_state * pgs, gs_fixed_rect * pbox)
  432. {
  433.     int code = gx_cpath_from_rectangle(pgs->clip_path, pbox);
  434.  
  435.     if (code < 0)
  436.     return code;
  437.     pgs->clip_path->rule = gx_rule_winding_number;
  438.     note_set_clip_path(pgs);
  439.     return 0;
  440. }
  441.  
  442. /* Set the clipping path to the current path, without intersecting. */
  443. /* This is very inefficient right now. */
  444. int
  445. gx_clip_to_path(gs_state * pgs)
  446. {
  447.     gs_fixed_rect bbox;
  448.     int code;
  449.  
  450.     if ((code = gx_path_bbox(pgs->path, &bbox)) < 0 ||
  451.     (code = gx_clip_to_rectangle(pgs, &bbox)) < 0 ||
  452.     (code = gs_clip(pgs)) < 0
  453.     )
  454.     return code;
  455.     note_set_clip_path(pgs);
  456.     return 0;
  457. }
  458.  
  459. /* Get the default clipping box. */
  460. int
  461. gx_default_clip_box(const gs_state * pgs, gs_fixed_rect * pbox)
  462. {
  463.     register gx_device *dev = gs_currentdevice(pgs);
  464.     gs_rect bbox;
  465.     gs_matrix imat;
  466.     int code;
  467.  
  468.     if (dev->ImagingBBox_set) {    /* Use the ImagingBBox, relative to default user space. */
  469.     gs_defaultmatrix(pgs, &imat);
  470.     bbox.p.x = dev->ImagingBBox[0];
  471.     bbox.p.y = dev->ImagingBBox[1];
  472.     bbox.q.x = dev->ImagingBBox[2];
  473.     bbox.q.y = dev->ImagingBBox[3];
  474.     } else {            /* Use the MediaSize indented by the HWMargins, */
  475.     /* relative to unrotated user space adjusted by */
  476.     /* the Margins.  (We suspect this isn't quite right, */
  477.     /* but the whole issue of "margins" is such a mess that */
  478.     /* we don't think we can do any better.) */
  479.     (*dev_proc(dev, get_initial_matrix)) (dev, &imat);
  480.     /* Adjust for the Margins. */
  481.     imat.tx += dev->Margins[0] * dev->HWResolution[0] /
  482.         dev->MarginsHWResolution[0];
  483.     imat.ty += dev->Margins[1] * dev->HWResolution[1] /
  484.         dev->MarginsHWResolution[1];
  485.     bbox.p.x = dev->HWMargins[0];
  486.     bbox.p.y = dev->HWMargins[1];
  487.     bbox.q.x = dev->MediaSize[0] - dev->HWMargins[2];
  488.     bbox.q.y = dev->MediaSize[1] - dev->HWMargins[3];
  489.     }
  490.     code = gs_bbox_transform(&bbox, &imat, &bbox);
  491.     if (code < 0)
  492.     return code;
  493.     /* Round the clipping box so that it doesn't get ceilinged. */
  494.     pbox->p.x = fixed_rounded(float2fixed(bbox.p.x));
  495.     pbox->p.y = fixed_rounded(float2fixed(bbox.p.y));
  496.     pbox->q.x = fixed_rounded(float2fixed(bbox.q.x));
  497.     pbox->q.y = fixed_rounded(float2fixed(bbox.q.y));
  498.     return 0;
  499. }
  500.